package com.infra.micro.quartz.domain.service.impl;

import com.github.pagehelper.PageHelper;
import com.infra.common.resp.Page;
import com.infra.common.resp.Response;
import com.infra.common.util.BizUtils;
import com.infra.micro.quartz.domain.service.QuartzJobLogService;
import com.infra.micro.quartz.domain.entity.QuartzJobLogPO;
import com.infra.micro.quartz.domain.repository.QuartzJobLogRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class QuartzJobLogServiceImpl implements QuartzJobLogService {

    @Resource
    private QuartzJobLogRepository quartzJobLogRepository;

    public boolean update(QuartzJobLogPO record) {
        return quartzJobLogRepository.update(record);
    }

    public boolean insert(QuartzJobLogPO record) {
        return quartzJobLogRepository.insert(record);
    }

    public Response<QuartzJobLogPO> queryById(Long logId) {
        QuartzJobLogPO record = quartzJobLogRepository.queryById(logId);
        return record == null ? Response.error("无效logId") : Response.success(record, "查询成功");
    }

    public Response<Integer> deleteById(Long logId) {
        int ret = quartzJobLogRepository.deleteById(logId);
        return Response.success(ret, "删除成功");
    }

    public Response<Integer> batchDeleteByIds(List<Long> logIds) {
        int ret = quartzJobLogRepository.batchDeleteByLogIds(logIds);
        return Response.success(ret, "删除成功");
    }

    public Response<Page<QuartzJobLogPO>> queryListByJobId(Long jobId, Page<QuartzJobLogPO> page) {
        BizUtils.buildPageData(page);
        page.getQuery().put("jobId", jobId);
        return queryListByConf(page);
    }

    public Response<Page<QuartzJobLogPO>> queryListByConf(Page<QuartzJobLogPO> page) {
        PageHelper.startPage(page.getPageIndex(), page.getPageSize(), StringUtils.isEmpty(page.getSortField()) ? null : page.getSortField() + " " + page.getSortType());
        List<QuartzJobLogPO> list = quartzJobLogRepository.queryListByConf(page.getQuery());
        page.build(list);

        return Response.success(page, "查询成功");
    }
}
